package main

import (
	"fmt"
	"sort"
	"strings"
)

func parent(path string) string {
	if path == "/" {
		return path
	}
	pivot := strings.LastIndexByte(path, '/')
	if pivot <= 0 {
		return "/"
	}
	return path[:pivot]

}

func removeSubfolders(folder []string) []string {
	var result []string
	m := make(map[string]struct{})
	sort.Slice(folder, func(i, j int) bool {
		return len(folder[i]) < len(folder[j])
	})
outer:
	for _, path := range folder {
		p := path
		for p != "/" {
			_, ok := m[p]
			if ok {
				continue outer
			} else {
				p = parent(p)
			}
		}
		m[path] = struct{}{}
		result = append(result, path)
	}
	return result
}

func main() {
	ret := removeSubfolders([]string{"/a", "/a/b", "/c/d", "/c/d/e", "/c/f"})
	fmt.Printf("%#v\n", ret)
}
